<template>
  <ComWrapper>
    <el-card shadow="never">
			<el-form ref="ruleFormRef" :model="form" label-width="100px">
				<el-form-item label="文件名" prop="filename">
					<el-input placeholder="文件名，默认test.xlsx" v-model="form.filename" />
				</el-form-item>
				<el-form-item label="是否自动宽" prop="autoWidth">
          <el-radio-group v-model="form.autoWidth">
            <el-radio :label="true" border>是</el-radio>
            <el-radio :label="false" border>否</el-radio>
          </el-radio-group>
				</el-form-item>
        <el-form-item label="文件类型" prop="type">
					<el-select v-model="form.type">
            <el-option
              v-for="item in ['xlsx', 'csv', 'txt']"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
				</el-form-item>
        <el-form-item>
          <el-button @click="handleDownload">导出</el-button>
        </el-form-item>
			</el-form>
		</el-card>
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading...">
      <el-table-column align="center" label="id">
        <template #default="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="title">
        <template #default="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="author" align="center">
        <template #default="scope">
          <el-tag>{{ scope.row.author }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="readings" align="center">
        <template #default="scope">
          {{ scope.row.readings }}
        </template>
      </el-table-column>
      <el-table-column label="date" align="center">
        <template #default="scope">
          {{ scope.row.date }}
        </template>
      </el-table-column>
      <el-table-column label="content" align="center">
        <template #default="scope">
          {{ scope.row.content }}
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="Date" width="220">
        <template #default="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column> -->
    </el-table>
  </ComWrapper>
</template>

<script setup>
import {ref} from 'vue'
import {parseTime} from '@/function/date'

const listLoading = ref(false)
const downloadLoading = ref(false)
const form = ref({
  filename: '',
  autoWidth: true,
  type: 'xlsx'
})
const list = [
  {
    id: 1,
    title: 'title--1',
    author: 'xjc',
    readings: 123,
    content: 'this is a content',
    date: '2020-10-2'
  },
  {
    id: 2,
    title: 'title--2',
    author: 'xjc',
    readings: 456,
    content: 'this is a content',
    date: '2020-10-2'
  },
  {
    id: 3,
    title: 'title--3',
    author: 'xjc',
    readings: 789,
    content: 'this is a content',
    date: '2020-10-2'
  }
]

function handleDownload() {
  downloadLoading.value = true
  import('@/plugins/excel').then(excel => {
    const tHeader = ['id', 'title', 'author', 'readings', 'date', 'content']
    const filterVal = ['id', 'title', 'author', 'readings', 'date', 'content']
    const data = formatJson(filterVal, list)
    const formVal = form.value
    excel.export_json_to_excel({
      header: tHeader,
      data,
      filename: formVal.filename,
      autoWidth: formVal.autoWidth,
      type: formVal.type
    })
    downloadLoading.value = false
  })
}
function formatJson(filterVal, jsonData) {
  return jsonData.map(v => filterVal.map(j => {
    if (j === 'timestamp') {
      return parseTime(v[j])
    } else {
      return v[j]
    }
  }))
}
</script>